package com.briup.bookstore.web.controller;

import com.briup.bookstore.common.model.entity.User;
import com.briup.bookstore.common.model.param.PageParam;
import com.briup.bookstore.common.response.Result;
import com.briup.bookstore.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    /**
     * 用户模块Service对象
     */
    private final UserService userService;

    /**
     * 新增用户信息
     *
     * @param
     * @return
     */
    @PostMapping
    public Result add(@RequestBody User userParam) {
        userService.add(userParam);
        return Result.ok();
    }

    /**
     * 分页+条件检索用户信息
     *
     * @param pageNum   当前页
     * @param pageSize  每页大小
     * @param username  用户名
     * @param status    用户状态
     * @param startTime 注册时间范围-开始时间
     * @param endTime   注册时间范围-结束时间
     * @return
     */
    @GetMapping(params = "page=true")
    public Result pageQuery(@RequestParam(value = "pageNum") int pageNum,
                            @RequestParam(value = "pageSize") int pageSize,
                            @RequestParam(value = "username", required = false) String username,
                            @RequestParam(value = "status", required = false) Integer status,
                            @RequestParam(value = "startTime", required = false) Date startTime,
                            @RequestParam(value = "endTime", required = false) Date endTime) {
        /* 封装分页参数 */
        PageParam pageParam = new PageParam()
                .pageNum(pageNum)
                .pageSize(pageSize);
        /* 封装查询条件参数 */
        User userParam = new User()
                .setUsername(username)
                .setStatus(status)
                .setStartTime(startTime)
                .setEndTime(endTime);
        return Result.ok(userService.pageQueryByClause(pageParam, userParam));
    }


    /**
     * 删除用户信息
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Integer> ids) {
        userService.delete(ids);
        return Result.ok();
    }


    /**
     * 根据ID查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        return Result.ok(userService.getById(id));
    }

    /**
     * 修改用户信息
     *
     * @param userParam
     * @return
     */
    @PutMapping
    public Result update(@RequestBody User userParam) {
        userService.update(userParam);
        return Result.ok();
    }

}
